package com.nidu.demo.tenant.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageQuery;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.tenant.dto.TenantListQry;
import com.nidu.demo.tenant.dto.TenantPageQry;
import com.nidu.demo.tenant.convertor.TenantConvertor;
import com.nidu.demo.tenant.gateway.TenantGateway;
import com.nidu.demo.tenant.mapper.TenantMapper;
import com.nidu.demo.tenant.mapper.dataobject.TenantDO;
import com.nidu.demo.tenant.model.Tenant;
import com.nidu.demo.tenant.model.TenantCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Set;
import java.util.List;

@Component
@RequiredArgsConstructor
public class TenantGatewayImpl implements TenantGateway {
    
    private final TenantMapper mapper;
    private final TenantConvertor convertor;

    @Override
    public Tenant create(Tenant tenant) {
        TenantDO tenantDO = convertor.toDataObject(tenant);
        mapper.insert(tenantDO);
        return convertor.toDomainObject(tenantDO);
    }

    @Override
    public Tenant update(Tenant tenant) {
        TenantDO tenantDO = convertor.toDataObject(tenant);
        mapper.updateById(tenantDO);
        return convertor.toDomainObject(tenantDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteByIds(ids);
    }

    @Override
    public Tenant getById(Long id) {
        TenantDO tenantDO = mapper.selectById(id);
        if(tenantDO == null){
            return null;
        }
        return convertor.toDomainObject(tenantDO);
    }

    @Override
    public Tenant getByName(String name) {
        TenantDO tenantDO = mapper.selectByName(name);
        if(tenantDO == null){
            return null;
        }
        return convertor.toDomainObject(tenantDO);
    }

    @Override
    public List<Tenant> listByCondition(TenantCriteria criteria) {
        LambdaQueryWrapper<TenantDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(criteria.getStatus() != null, TenantDO::getStatus, criteria.getStatus());
        wrapper.in(!CollUtil.isEmpty(criteria.getTenantIds()), TenantDO::getId, criteria.getTenantIds());

        wrapper.like(!StrUtil.isEmpty(criteria.getName()), TenantDO::getName, criteria.getName());

        List<TenantDO> tenantDOS = mapper.selectList(wrapper);
        if(CollUtil.isEmpty(tenantDOS)){
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(tenantDOS);
    }

    @Override
    public PageResponse<Tenant> pageByCondition(TenantCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<TenantDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(criteria.getStatus() != null, TenantDO::getStatus, criteria.getStatus());

        LocalDateTime[] createTime = criteria.getCreateTime();
        if (createTime != null) {
            if (createTime.length == 1 && createTime[0] != null) {
                wrapper.ge(TenantDO::getCreateTime, createTime[0]);
            } else if (createTime.length > 1 && createTime[0] != null && createTime[1] != null) {
                wrapper.between(TenantDO::getCreateTime, createTime[0], createTime[1]);
            }
        }

        wrapper.like(!StrUtil.isEmpty(criteria.getName()), TenantDO::getName, criteria.getName());
        // 创建分页对象，确保使用 TenantDO
        Page<TenantDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());

        // 排序字段
        if (!StrUtil.isEmpty(criteria.getOrderBy())) {
            if (PageQuery.DESC.equals(criteria.getOrderDirection())) {
                page.addOrder(OrderItem.desc(criteria.getOrderBy()));
            } else {
                page.addOrder(OrderItem.asc(criteria.getOrderBy()));
            }
        }
        // 执行分页查询
        mapper.selectPage(page, wrapper);
        List<Tenant> tenants = convertor.toDomainObjects(page.getRecords());
        return PageResponse.of(tenants, (int)page.getTotal(), (int)page.getSize(), (int)page.getCurrent());
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        TenantDO tenantDO = new TenantDO();
        tenantDO.setId(id);
        tenantDO.setStatus(status);
        mapper.updateById(tenantDO);
    }
} 